﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace CustomControls.MessageBox
{
    /// <summary>
    /// 自定义MessageBox消息框弹出窗口  
    /// </summary>
    public sealed class NewMessageBox
    {
        private static object lockObj = new object();
        public static double Top = 0;
        public static double Bottom = 0;
        public static double Left = 0;
        public static double Right = 0;
        #region 私有方法

        /// <summary>
        /// 获取消息框中显示的图标样式
        /// </summary>
        /// <param name="msgImage">图标样式类型</param>
        /// <param name="win">弹出窗口对象</param>
        /// <returns></returns>
        private static Style GetMessageImageStyle(MessageBoxImage msgImage, Window win)
        {
            Style retValue = null;
            switch (msgImage)//选择图片状态
            {
                case MessageBoxImage.Warning:
                    retValue = win.TryFindResource("Common_Bmp_mMessageBoxWarning") as System.Windows.Style;
                    break;
                case MessageBoxImage.Error:
                    retValue = win.TryFindResource("Common_Bmp_mMessageBoxError") as System.Windows.Style;
                    break;
                case MessageBoxImage.Question:
                    retValue = win.TryFindResource("Common_Bmp_mMessageBoxQuestion") as System.Windows.Style;
                    break;
                case MessageBoxImage.Information:
                    retValue = win.TryFindResource("Common_Bmp_mMessageBoxInformation") as System.Windows.Style;
                    break;
            }

            return retValue;
        }

        /// <summary>
        /// 构造窗体
        /// </summary>
        /// <param name="content"></param>
        /// <param name="title">窗体标题</param>
        /// <param name="button">提示内容</param>
        /// <param name="icon">按钮类型</param>
        /// <param name="result">图标类型</param>
        /// <returns>默认结果</returns>
        private static MessageBoxResult CreateForm(string content, string title, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult result, Window owner, int height = 0, int width = 0, int textWidth = 0)
        {
            lock (lockObj)
            {
                MessageBoxWindow msgWin = new MessageBoxWindow(content);
                msgWin.Title = title;
                switch (button) //选择按钮状态
                {
                    case MessageBoxButton.YesNo:
                        msgWin.btnYes.Visibility = msgWin.btnNo.Visibility = Visibility.Visible;
                        msgWin.btnConfirm.Visibility = Visibility.Collapsed;
                        break;
                    case MessageBoxButton.OKCancel:
                        msgWin.btnCancel.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.YesNoCancel:
                        msgWin.btnCancel.Visibility = msgWin.btnYes.Visibility = msgWin.btnNo.Visibility = Visibility.Visible;
                        msgWin.btnConfirm.Visibility = Visibility.Collapsed;
                        break;
                    default:
                        msgWin.btnConfirm.Visibility = Visibility.Visible;
                        break;
                }
                //设置对话框图标
                msgWin.MessageboxImagebg.Style = GetMessageImageStyle(icon, msgWin);
                if (msgWin.MessageboxImagebg.Style == null)
                {
                    msgWin.MessageboxImagebg.Visibility = Visibility.Collapsed;
                }
                msgWin.Width = (width == 0 ? (msgWin.MessageInfo.Width + 140) : width);
                msgWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                msgWin.Height = (height == 0 ? 150 : height);
                if (textWidth != 0)
                {
                    msgWin.MessageInfo.Width = textWidth;
                }
                if (Top != 0 || Bottom != 0 || Left != 0 || Right != 0)
                {
                    msgWin.MessageInfo.Margin = new Thickness() { Top = Top, Bottom = Bottom, Left = Left, Right = Right };
                }
                msgWin.Owner = owner;
                msgWin.Topmost = true;
                msgWin.ShowDialog();
                Top = 0;
                Bottom = 0;
                Left = 0;
                Right = 0;
                return msgWin.MessageBoxResult == MessageBoxResult.None ? result : msgWin.MessageBoxResult;
            }
        }

        /// <summary>
        /// 弹框选择
        /// </summary>
        /// <param name="content"></param>
        /// <param name="title"></param>
        /// <param name="image"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        private static MessageBoxResult AutoMessageShowCreate(string content, string title, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult result, Window owner, int height = 0, int width = 0, int textWidth = 0)
        {
            lock (lockObj)
            {
                MessageBoxShowSelectInfo msgWin = new MessageBoxShowSelectInfo(content);
                msgWin.Title = title;
                switch (button) //选择按钮状态
                {
                    case MessageBoxButton.YesNo:
                        msgWin.btnYes.Visibility = msgWin.btnNo.Visibility = Visibility.Visible;
                        msgWin.btnConfirm.Visibility = Visibility.Collapsed;
                        break;
                    case MessageBoxButton.OKCancel:
                        msgWin.btnCancel.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.YesNoCancel:
                        msgWin.btnCancel.Visibility = msgWin.btnYes.Visibility = msgWin.btnNo.Visibility = Visibility.Visible;
                        msgWin.btnConfirm.Visibility = Visibility.Collapsed;
                        break;
                    default:
                        msgWin.btnConfirm.Visibility = Visibility.Visible;
                        break;
                }
                //设置对话框图标
                msgWin.MessageboxImagebg.Style = GetMessageImageStyle(icon, msgWin);
                if (msgWin.MessageboxImagebg.Style == null)
                {
                    msgWin.MessageboxImagebg.Visibility = Visibility.Collapsed;
                }
                msgWin.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                if (Top != 0 || Bottom != 0 || Left != 0 || Right != 0)
                {
                    msgWin.MessageInfo.Margin = new Thickness() { Top = Top, Bottom = Bottom, Left = Left, Right = Right };
                }
                msgWin.Owner = owner;
                msgWin.Topmost = true;
                msgWin.ShowDialog();
                Top = 0;
                Bottom = 0;
                Left = 0;
                Right = 0;
                return msgWin.MessageBoxResult == MessageBoxResult.None ? result : msgWin.MessageBoxResult;
            }
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 显示一个消息框，该消息框包含消息并返回结果
        /// </summary>
        /// <param name="content">显示的文本</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string content, Window owner=null)
        {
            return CreateForm(content, "系统提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.None,owner);
        }

        public static MessageBoxResult AutoShowMsg(string content, Window owner = null)
        {
            return AutoMessageShowCreate(content, "系统提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.None, owner);
        }


        /// <summary>
        /// 显示一个消息框，该消息框包含消息并返回结果
        /// </summary>
        /// <param name="content">显示的文本</param>
        /// <param name="title">消息框标题</param>
        /// <param name="dependencyObject">弹出该消息框的控件或窗体</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string content, string title, Window owner = null)
        {
            return CreateForm(content, title, MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.None, owner);
        }

        public static MessageBoxResult AutoShowMsg(string content, string title, Window owner = null)
        {
            return AutoMessageShowCreate(content, title, MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.None, owner);
        }

        /// <summary>
        /// 显示一个消息框，该消息框包含消息并返回结果
        /// </summary>
        /// <param name="content">显示的文本</param>
        /// <param name="title">消息框标题</param>
        /// <param name="button">消息框上显示的按钮</param>
        /// <param name="dependencyObject">弹出该消息框的控件或窗体</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string content, string title, MessageBoxButton button, Window owner = null)
        {
            return CreateForm(content, title, button, MessageBoxImage.Information, MessageBoxResult.None, owner);
        }

        public static MessageBoxResult AutoShowMsg(string content, string title, MessageBoxButton button, Window owner = null)
        {
            return AutoMessageShowCreate(content, title, button, MessageBoxImage.Information, MessageBoxResult.None, owner);
        }

        /// <summary>
        /// 显示一个消息框，该消息框包含消息并返回结果
        /// </summary>
        /// <param name="content">显示的文本</param>
        /// <param name="title">消息框标题</param>
        /// <param name="button">消息框上显示的按钮</param>
        /// <param name="image">消息框上显示的图标</param>
        /// <param name="dependencyObject">弹出该消息框的控件或窗体</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string content, string title, MessageBoxButton button, MessageBoxImage image, Window owner = null)
        {
            return CreateForm(content, title, button, image, MessageBoxResult.None, owner);
        }

        public static MessageBoxResult AutoShowMsg(string content, string title, MessageBoxButton button, MessageBoxImage image, Window owner = null)
        {
            return AutoMessageShowCreate(content, title, button, image, MessageBoxResult.None, owner);
        }


        /// <summary>
        /// 显示一个消息框，该消息框包含消息并返回结果
        /// </summary>
        /// <param name="content">显示的文本</param>
        /// <param name="title">消息框标题</param>
        /// <param name="button">消息框上显示的按钮</param>
        /// <param name="image">消息框上显示的图标</param>
        /// <param name="result">消息框上返回结果</param>
        /// <param name="dependencyObject">弹出该消息框的控件或窗体</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string content, string title, MessageBoxButton button, MessageBoxImage image, MessageBoxResult result, Window owner = null)
        {
            return CreateForm(content, title, button, image, result, owner);
        }

        public static MessageBoxResult AutoShowMsg(string content, string title, MessageBoxButton button, MessageBoxImage image, MessageBoxResult result, Window owner = null)
        {
            return AutoMessageShowCreate(content, title, button, image, result, owner);
        }

        /// <summary>
        ///  显示一个消息框
        /// </summary>
        /// <param name="content">显示的文本</param>
        /// <param name="title">消息框标题</param>
        /// <param name="button">消息框上显示的按钮</param>
        /// <param name="image">消息框上显示的图标</param>
        /// <param name="height">窗体高度</param>
        /// <param name="width">窗体宽度</param>
        /// <param name="thickness">文本位置</param>
        /// <param name="textWidth">文本宽度</param>
        /// <returns></returns>
        public static MessageBoxResult Show(string content, string title, MessageBoxButton button, MessageBoxImage image, int height, int width, Thickness thickness, int textWidth)
        {
            Top = thickness.Top;
            Bottom = thickness.Bottom;
            Left = thickness.Left;
            Right = thickness.Right;
            return CreateForm(content, title, button, image, MessageBoxResult.None, null, height, width, textWidth);
        }

        public static MessageBoxResult AutoShowMsg(string content, string title, MessageBoxButton button, MessageBoxImage image, int height, int width, Thickness thickness, int textWidth)
        {
            Top = thickness.Top;
            Bottom = thickness.Bottom;
            Left = thickness.Left;
            Right = thickness.Right; 
            return AutoMessageShowCreate(content, title, button, image, MessageBoxResult.None, null, height, width, textWidth);
        }
        #endregion

    }
}
